package jsu.lipengcheng.trafficnet.Util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Component
public class OssUtil {
    private String endpoint="oss-cn-beijing.aliyuncs.com";
    private String accessKeyId="LTAI5t5fMewau3cRskLZi4yK";
    private String accessKeySecret="QYcWV3Zy7GYYxBKCNYAfaoLHFkx5H2";
    private String bucketName="traffic-project";
    public String uplod(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String oldname = file.getOriginalFilename();
        int i = oldname.lastIndexOf(".");
        String substring = oldname.substring(i);
        String string = UUID.randomUUID().toString();
        String name=string+substring;
        OSS oss=new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        oss.putObject(bucketName,name,inputStream);
        String url="http://"+bucketName+"."+endpoint+"/"+name;
        return url;
    }
}
